//
//  AnimationVC.m
//  核心动画
//
//  Created by chichaojituan521 on 2017/5/26.
//  Copyright © 2017年 linch. All rights reserved.
//

#import "AnimationVC2.h"
#import "ZhenAnimation.h"
@interface AnimationVC2 ()
{
    NSMutableArray *imageArray;
    int itimer;
}
@end

@implementation AnimationVC2

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUIMethod];
    NSArray *arrName = @[@"振荡",@"往返1",@"往返2",@"UIView组",@"转场换图",@"侦动画1",@"侦动画2",@"UIView翻页",@"view翻页",@"方path",@"圆path",@"自定path",@"icon抖动",@"imgv转场",@"侦动画封装",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"返回"];
    
    imageArray=[[NSMutableArray alloc]init];
    for (int i=0 ; i<40; i++)
    {
        NSString *name=[NSString stringWithFormat:@"eat_%d.jpg",i];
        
        UIImage *eatImage=[UIImage imageNamed:name ];
        [imageArray addObject:eatImage];
    }
    
    for (int i = 0;i<25 ; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        btn.backgroundColor = [UIColor orangeColor];
        float JG = (self.view.frame.size.width - 70*5)/6.0;
        float btnx = JG + (JG + 70)*(i%5);
        float btny = 500 + (JG+30)*(i/5);
        btn.frame = CGRectMake(btnx, btny, 70, 30);
        [self.view addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arrName[i] forState:UIControlStateNormal];
    }

}
-(void)createUIMethod{
    self.view.backgroundColor = [UIColor whiteColor];
    _imgv1 = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-300)/2.0, 40, 60, 60)];
    [self.view addSubview:_imgv1];
    _imgv1.image = [UIImage imageNamed:@"1.jpg"];
    _imgv1.backgroundColor = [UIColor orangeColor];
    
    
    _imgv2 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 400, 30, 30)];
    [self.view addSubview:_imgv2];
    _imgv2.image = [UIImage imageNamed:@"btnbtn"];
    
    
    _view1 = [[UIView alloc]initWithFrame:CGRectMake(200, 320, 100, 100)];
    [self.view addSubview:_view1];
    _view1.backgroundColor = [UIColor redColor];
    
    
    _view2 = [[UIView alloc]initWithFrame:CGRectMake(200, 400, 100, 100)];
    [self.view addSubview:_view2];
    _view2.backgroundColor = [UIColor greenColor];

}
-(void)btnClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
            [self springAni];
            break;
        case 1:
            [self uiviewYiDongAni];
            break;
        case 2:
            [self paomadengAni];
            break;
        case 3:
            [self viewAnimationGroup];

            break;
        case 4:
            [self onlyTransitionAni];
            break;
        case 5:
            [self zhenDongHuaEat1];
            break;
        case 6:
            [self zhenDongHuaEat2];
            break;
        case 7:
            [self filUPAni];
            break;
        case 8:
            [self zhuanchangDongHua];
            break;
        case 9:
            [self valueKeyframeAni];
            break;
        case 10:
            [self pathKeyframeAni];
            break;
        case 11:
            [self pathAnimationMethod];
            break;
        case 12:
            [self iconDouDong];
            break;
        case 13:
            [self jianBianXiaoGuo];
            break;
        case 14:
            [self zhenAnimationFengZhuang];
            break;
        case 15:
//            [self contentsAni];
            break;
        case 16:
//            [self shadowColorAni];
            break;
        case 17:
//            [self shadowOffset];
            break;
        case 18:
//            [self shadowRadius];
            break;
        case 19:
//            [self shadowOpacity];
            break;
        case 20:
//            [self sizeAnimationMethod];
            break;
        case 21:
//            [self sizeheightAnimationMethod];
            break;
        case 22:
//            [self sizewidthAnimationMethod];
            break;
        case 23:
//            [self backgroundColorAnimation];
            
            break;
        case 24:
            [self jumpBackLastVC];
            
            break;
            
            
        default:
            break;
    }
}

-(void)jumpBackLastVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --震动/移动
- (void)springAni {
    CGRect rectaa = self.view2.frame;
    //usingSpringWithDamping: 震动效果, 范围0~1, 数值越小震动效果越明显
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:1 options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        CGRect rectx = self.view2.frame;
        rectx.origin.y -= 200;
        self.view2.frame = rectx;
        
    } completion:^(BOOL finished) {
        self.view2.frame = rectaa;
        
    }];
}

#pragma mark --往返移动
//*************************参数可以同时加多个
- (void)uiviewYiDongAni {
    //    CGRect rectaa = self.view2.frame;
    [UIView animateWithDuration:2 delay:0.0 usingSpringWithDamping:1 initialSpringVelocity:0.5 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut animations:^{
        CGRect rectx = self.view2.frame;
        rectx.origin.x += 200;
        self.view2.frame = rectx;
        
    } completion:^(BOOL finished) {
        //        self.view2.frame = rectaa;
    }];
    
}
#pragma mark --往返移动   跑马灯
-(void)paomadengAni
{
    [UIView beginAnimations:nil context:nil];
    //设置动画反向
    [UIView setAnimationRepeatAutoreverses:YES];
    //设置动画重复次数
    [UIView setAnimationRepeatCount:LONG_MAX];
    //设置移动速度
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    //设置动画时间
    [UIView setAnimationDuration: 3];
    _imgv1.frame=CGRectMake(320, 40, 100, 60);
    
    
    [UIView commitAnimations];
}
//关键帧动画, 支持属性关键帧, 不支持路径关键帧
#pragma mark --UIView动画组
-(void)viewAnimationGroup
{
    // frameStartTime 开始时间占总时间的百分比
    // relativeDuration 每一个小的动画占总时间的百分比
    
    [UIView animateKeyframesWithDuration:5 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeCubic animations:^{
        
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.2 animations:^{
            CGRect rectx = self.imgv2.frame;
            rectx.origin.x += 40;
            rectx.origin.y -= 10;
            self.imgv2.frame = rectx;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2 relativeDuration:0.2 animations:^{
            CGRect rectx = self.imgv2.frame;
            rectx.origin.x += 30;
            rectx.origin.y -= 10;
            self.imgv2.frame = rectx;
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.4 relativeDuration:0.2 animations:^{
            CGRect rectx = self.imgv2.frame;
            rectx.origin.x += 20;
            rectx.origin.y -= 10;
            self.imgv2.frame = rectx;
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.6 relativeDuration:0.2 animations:^{
            CGRect rectx = self.imgv2.frame;
            rectx.origin.x += 10;
            rectx.origin.y -= 10;
            self.imgv2.frame = rectx;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.8 relativeDuration:0.2 animations:^{
            CGRect rectx = self.imgv2.frame;
            rectx.origin.x += 10;
            rectx.origin.y -= 20;
            self.imgv2.frame = rectx;
        }];
        
        
        
    } completion:^(BOOL finished) {
    }];
}

#pragma mark --  单个视图的转场动画
- (void)onlyTransitionAni {
    [UIView transitionWithView:self.imgv1 duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.imgv1.image = [UIImage imageNamed:@"timg1.jpg"];
    } completion:^(BOOL finished) {
        self.imgv1.image = [UIImage imageNamed:@"timg4.jpg"];
    }];
}
#pragma mark --  侦动画
-(void)zhenDongHuaEat1
{
    _imgv1.animationImages =imageArray;
    _imgv1.animationDuration=0.1*imageArray.count;
    _imgv1.animationRepeatCount=1;
    [_imgv1 startAnimating];
   
}
#pragma mark --  侦动画 定时器
-(void)zhenDongHuaEat2
{
    NSTimer *timer;
    if (timer) {
        [timer invalidate];
        timer = nil;
        itimer = 0;
    }
   timer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
    
    [timer fire];
}
-(void) timerClick{
    if (itimer<40) {
        _imgv1.image = imageArray[itimer];
        itimer++;
    }
    else
    {
        itimer = 0;
    }
}
#pragma mark --  UIView翻页动画

- (void)filUPAni {
    [UIView beginAnimations:@"FlipAni" context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationWillStartSelector:@selector(startAni:)];
    [UIView setAnimationDidStopSelector:@selector(stopAni:)];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
    
    self.imgv1.image = [UIImage imageNamed:@"timg1.jpg"];
    [UIView commitAnimations];
}
- (void)startAni:(NSString *)aniId {
    NSLog(@"%@ start", aniId);
}
- (void)stopAni:(NSString *)aniID {
    NSLog(@"%@ stop", aniID);
}

//
#pragma mark --  view的转场动画

- (void)zhuanchangDongHua {
    
    [UIView transitionFromView:self.view1 toView:_view2 duration:1 options:UIViewAnimationOptionTransitionCurlUp completion:^(BOOL finished) {
        
    }];
}
#pragma mark --  动画沿正方形运动
- (void)valueKeyframeAni {
    CAKeyframeAnimation * ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ani.duration = 4.0;
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
//    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(150, 200)];
    NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(250, 200)];
    NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(250, 300)];
    NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(150, 300)];
    NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(150, 200)];
    ani.values = @[value1, value2, value3, value4, value5];
    
    
    ani.keyTimes = @[[NSNumber numberWithFloat:0.0],
                     [NSNumber numberWithFloat:0.6],
                     [NSNumber numberWithFloat:0.7],
                     [NSNumber numberWithFloat:0.8],
                     [NSNumber numberWithFloat:1]];
    ani.timingFunctions = @[
                            [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                            [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                            [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],
                            [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    //重复次数
    ani.repeatCount = 2;
    
    [self.imgv2.layer addAnimation:ani forKey:@"PostionKeyframeValueAni"];
}
#pragma mark --  设置path使其绕圆圈运动
- (void)pathKeyframeAni {
    CAKeyframeAnimation * ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddEllipseInRect(path, NULL, CGRectMake(130, 200, 100, 100));
    
    ani.path = path;
    CGPathRelease(path);
    ani.duration = 4.0;
    ani.removedOnCompletion = NO;
    ani.fillMode = kCAFillModeForwards;
    [self.imgv2.layer addAnimation:ani forKey:@"PostionKeyframePathAni"];
}
#pragma mark --  设置自定义path使其运动
-(void)pathAnimationMethod
{
    /**
     CAKeyframeAnimation
     
     在画线的时候，方法的内部默认创建一个path。它把路径都放到了path里面去。
     1.创建路径  CGMutablePathRef 调用该方法相当于创建了一个路径，这个路径用来保存绘图信息。
     2.把绘图信息添加到路径里边。
     以前的方法是点的位置添加到ctx（图形上下文信息）中，ctx 默认会在内部创建一个path用来保存绘图信息。
     在图形上下文中有一块存储空间专门用来存储绘图信息，其实这块空间就是CGMutablePathRef。
     3.把路径添加到上下文中。
     */
    CGMutablePathRef path = CGPathCreateMutable();
    
    CGPathMoveToPoint(path,NULL,50.0,120.0);
//    CGPathAddLineToPoint(path, NULL, 300, 488);
    CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,150.0,120.0);
    CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,250.0,120.0);
    CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,350.0,120.0);
    CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,450.0,120.0);
    

    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = path;
    animation.duration = 3;
    CFRelease(path);
    
    [self.imgv2.layer addAnimation:animation forKey:@"path"];
}

#pragma mark --  icon抖动
-(void)iconDouDong
{

    CAKeyframeAnimation *animation; animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 0.2;
    animation.repeatCount = 10000;
    // Try to get the animationto begin to start with a small offset // that makes it shake out of sync withother layers. srand([[NSDate date] timeIntervalSince1970]);
    float rand = (float)random();
    [animation setBeginTime:CACurrentMediaTime() + rand * .0000000001];
    NSMutableArray *values =[NSMutableArray array]; // Turn right
    [values addObject:DegreesToNumber(-2)]; // Turn left
    [values addObject:DegreesToNumber(2)]; // Turn right
    [values addObject:DegreesToNumber(-2)]; // Set the values for the animation
    animation.values = values;
    
    [self.imgv1.layer addAnimation:animation forKey:@"dd"];
}
//c的方法
NSNumber*DegreesToNumber(CGFloat degrees)
{
    return [NSNumber numberWithFloat: DegreesToRadians(degrees)];
}
CGFloat DegreesToRadians(CGFloat degrees)
{
    return degrees * M_PI / 180;
}

#pragma mark --  imgv转场效果
-(void)jianBianXiaoGuo
{
    CATransition * ani = [CATransition animation];
//    ani.type = kCATransitionFade;
//    ani.type = @"oglFlip";
    ani.type = @"rippleEffect";
    
    //私用API                     效果                      方向
//    cube	                     立方体翻转效果  		        是
//    oglFlip	                 翻转效果	        	        是
//    suckEffect	             收缩效果	                	否
//    rippleEffect	             水滴波纹效果	    	        否
//    pageCurl	                 向上翻页效果	            	是
//    pageUnCurl	             向下翻页效果	            	是
//    cameralIrisHollowOpen 	 摄像头打开效果	        	否
//    cameraIrisHollowClose 	 摄像头关闭效果
// **************************   公有
//     kCATransitionFade 渐变
//    kCATransitionMoveIn 覆盖
//    kCATransitionPush 推出
//    kCATransitionReveal 揭开
    
    
    ani.subtype = kCATransitionFromLeft;
//    kCATransitionFromRight 从右边
//    kCATransitionFromLeft 从左边
//    kCATransitionFromTop 从顶部
//    kCATransitionFromBottom 从底部
    ani.duration = 1.5;
    self.imgv1.image = [UIImage imageNamed:@"22.jpg"];
    [self.imgv1.layer addAnimation:ani forKey:@"transitionAni"];
}

- (void)zhenAnimationFengZhuang
{
    [ZhenAnimation animationWithImgv:_imgv1 pics:imageArray];
}





@end
